package com.chat.common.user.service.adapter;

import com.chat.common.user.domain.entity.User;
import com.chat.common.user.domain.entity.UserApply;
import com.chat.common.user.domain.entity.UserFriend;
import com.chat.common.user.domain.enums.ApplyReadStatusEnum;
import com.chat.common.user.domain.enums.ApplyStatusEnum;
import com.chat.common.user.domain.enums.ApplyTypeEnum;
import com.chat.common.user.domain.vo.req.friend.FriendApplyReq;
import com.chat.common.user.domain.vo.resp.friend.FriendApplyResp;
import com.chat.common.user.domain.vo.resp.friend.FriendResp;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

public class FriendAdapter {
    public static List<FriendResp> buildFriend(List<UserFriend> list, List<User> userList) {
        //好友的状态
        Map<Long, User> userMap = userList.stream().collect(Collectors.toMap(User::getId, user->user));
        return list.stream().map(userFriend -> {
            FriendResp resp = new FriendResp();
            resp.setUid(userFriend.getFriendUid());
            //遍历出friendId的user中的在线状态
            User user = userMap.get(userFriend.getFriendUid());
            if (Objects.nonNull(user)) {
                resp.setActiveStatus(user.getActiveStatus());
            }
            return resp;
        }).collect(Collectors.toList());
    }

    public static UserApply buildApplyFriend(Long uid, FriendApplyReq req) {
        return UserApply.builder()
                .uid(uid)
                .targetId(req.getTargetUid())
                .msg(req.getMsg())
                .status(ApplyStatusEnum.WAIT_APPROVAL.getCode())
                .type(ApplyTypeEnum.ADD_FRIEND.getCode())
                .readStatus(ApplyReadStatusEnum.UNREAD.getCode())
                .build();
    }

    public static UserFriend buildApplyApprove(Long uid, Long friendUid) {
        return UserFriend.builder()
                .uid(uid)
                .friendUid(friendUid)
                .deleteStatus(0)
                .build();
    }

    public static List<FriendApplyResp> buildFriendApplyList(List<UserApply> records) {
        return records.stream().map(k -> FriendApplyResp.builder()
                .applyId(k.getId())
                .uid(k.getUid())
                .msg(k.getMsg())
                .status(k.getStatus())
                .type(k.getType()).build()).collect(Collectors.toList());
    }
}
